package com.hiof.settings.user;

import com.hiof.beans.UserInfo;
import com.hiof.data.MoraService;

import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: manish
 * Date: 3/23/12
 * Time: 1:40 AM
 * Class to save the user information in the temporary memory
 */
public class UserConfiguration {

    private int userId = 0;
    private String userName = "";
    private String password = "";
    private int attendantId = 0;
    private String attendantCode = "";
    private String fullName = "";
    private String profilePicture = "";
    private int routeId = 0;
    private TypeOfAttendant typeOfAttendant;
    private ArrayList<UserInfo> teammates = new ArrayList<UserInfo>();
    private static UserConfiguration instance;

    public UserConfiguration() {
    }

    public static UserConfiguration getInstance() {
        if (instance == null) {
            instance = new UserConfiguration();
        }
        return instance;
    }

    public void destroy() {
        if (instance != null) {
            instance = null;
            //MoraService.getInstance().deleteConfigurationInstace();
        }
    }

    public void saveInstanceToDatabase() {
        MoraService.getInstance().saveUserConfigurationInstance(instance);
    }

    public int getUserId() {
        if (instance != null && userId == 0) {
            UserConfiguration backupInstance = MoraService.getInstance().getUserConfigurationInstance();
            if (backupInstance == null)
                return 0;
            userId = backupInstance.getUserId();
            instance = backupInstance;
        }
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
        /* if (this.userId != 0) {
            MoraService.getInstance().saveUserConfigurationInstance(instance);
        }*/
    }

    public String getProfilePicture() {
        if (instance != null && (profilePicture == null || profilePicture.equals(""))) {
            UserConfiguration backupInstance = MoraService.getInstance().getUserConfigurationInstance();
            if (backupInstance == null)
                return "";
            profilePicture = backupInstance.getProfilePicture();
            instance = backupInstance;
        }
        return profilePicture;
    }

    public void setProfilePicture(String profilePicture) {
        this.profilePicture = profilePicture;
        /* if (this.profilePicture != null && !this.profilePicture.equals("")) {
            MoraService.getInstance().saveUserConfigurationInstance(instance);
        }*/
    }

    public String getFullName() {
        if (instance != null && (fullName == null || fullName.equals(""))) {
            UserConfiguration backupInstance = MoraService.getInstance().getUserConfigurationInstance();
            if (backupInstance == null)
                return "";
            fullName = backupInstance.getFullName();
            instance = backupInstance;
        }
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
        /* if (instance != null && (this.fullName != null && !this.fullName.equals("")))
        MoraService.getInstance().saveUserConfigurationInstance(instance);*/
    }

    public String getAttendantCode() {
        if (instance != null && (attendantCode == null || attendantCode.equals(""))) {
            UserConfiguration backupInstance = MoraService.getInstance().getUserConfigurationInstance();

            if (backupInstance == null)
                return "";
            attendantCode = backupInstance.getAttendantCode();
            instance = backupInstance;
        }
        return attendantCode;
    }

    public void setAttendantCode(String attendantCode) {
        this.attendantCode = attendantCode;
        /*if (this.attendantCode != null && !this.attendantCode.equals(""))
            MoraService.getInstance().saveUserConfigurationInstance(instance);*/
    }

    public String getUserName() {
        if (instance != null && (userName == null || userName.equals(""))) {
            UserConfiguration backupInstance = MoraService.getInstance().getUserConfigurationInstance();
            if (backupInstance == null)
                return "";
            userName = backupInstance.getUserName();
            instance = backupInstance;
        }
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
        /*if (this.userName != null && !this.userName.equals(""))
            MoraService.getInstance().saveUserConfigurationInstance(instance);*/
    }

    public String getPassword() {
        if (instance != null && (password == null || password.equals(""))) {
            UserConfiguration backupInstance = MoraService.getInstance().getUserConfigurationInstance();
            if (backupInstance == null)
                return "";
            password = backupInstance.getPassword();
            instance = backupInstance;
        }
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
        /*if (this.password != null && !this.password.equals(""))
            MoraService.getInstance().saveUserConfigurationInstance(instance);*/
    }

    public int getAttendantId() {
        if (instance != null && attendantId == 0) {
            UserConfiguration backupInstance = MoraService.getInstance().getUserConfigurationInstance();
            if (backupInstance == null)
                return 0;
            attendantId = backupInstance.getAttendantId();
            instance = backupInstance;
        }
        return attendantId;
    }

    public void setAttendantId(int attendantId) {
        this.attendantId = attendantId;
        /*if (this.attendantId != 0)
            MoraService.getInstance().saveUserConfigurationInstance(instance);*/
    }

    public TypeOfAttendant getTypeOfAttendant() {
        if (instance != null && typeOfAttendant == null) {
            UserConfiguration backupInstance = MoraService.getInstance().getUserConfigurationInstance();
            if (backupInstance == null)
                return null;
            typeOfAttendant = backupInstance.getTypeOfAttendant();
            instance = backupInstance;
        }

        return typeOfAttendant;
    }

    public void setTypeOfAttendant(TypeOfAttendant typeOfAttendant) {
        this.typeOfAttendant = typeOfAttendant;
        /*if (this.typeOfAttendant != null)
            MoraService.getInstance().saveUserConfigurationInstance(instance);*/
    }

    public ArrayList<UserInfo> getTeammates() {
        if (instance != null && (teammates == null || teammates.size() == 0)) {
            UserConfiguration backupInstance = MoraService.getInstance().getUserConfigurationInstance();
            if (backupInstance == null)
                return null;
            teammates = backupInstance.getTeammates();
            instance = backupInstance;
        }

        return teammates;
    }

    public void setTeammates(ArrayList<UserInfo> teammates) {
        this.teammates = teammates;
        /* if (this.teammates != null && this.teammates.size() > 0)
        MoraService.getInstance().saveUserConfigurationInstance(instance);*/
    }

    public int getRouteId() {
        if (instance != null && routeId == 0) {
            UserConfiguration backupInstance = MoraService.getInstance().getUserConfigurationInstance();
            if (backupInstance == null)
                return 0;
            routeId = backupInstance.getRouteId();
            instance = backupInstance;
        }
        return routeId;
    }

    public void setRouteId(int routeId) {
        this.routeId = routeId;
        /*if (this.routeId != 0)
            MoraService.getInstance().saveUserConfigurationInstance(instance);*/
    }

    public String getUserNameByUserId(int userId) {
        teammates = getTeammates();
        for (UserInfo teammate : teammates) {
            if (teammate.getUserId() == userId) {
                return teammate.getFullname();
            }
        }
        return "support";
    }

    public String getSupportMobilePhoneByIndex(int index) {
        teammates = getTeammates();
        if (teammates.size() >= (index + 1)) {
            return teammates.get(index).getMobilePhone();
        }
        return "";
    }

    public String getSupportFullnameByIndex(int index) {
        teammates = getTeammates();
        return teammates.get(index).getFullname();
    }

    public UserInfo getTeammateByUserId(int userId) {
        teammates = getTeammates();
        for (UserInfo teammate : teammates) {
            if (teammate.getUserId() == userId)
                return teammate;
        }
        return null;
    }

    public UserInfo getAthleteWhenUserIsSupport() {
        teammates = getTeammates();
        if (getTypeOfAttendant() != TypeOfAttendant.SUPPORT) {
            return null;
        }
        for (UserInfo teammate : teammates) {
            if (teammate.getTypeOfAttendant() == TypeOfAttendant.ATHLETE)
                return teammate;
        }
        return null;
    }
}

